package sort;

import java.util.Arrays;

/**
 * 快速排序 以第一个数作为基准
 */
public class QuickSortDemo {
    public static void main(String[] args) {
        int[] arr = {4, 8, 0, 7, 2, 3, 5, 9, 6, 1};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));

    }
    public static void quickSort(int[] arr, int left, int right) {
        if(left>=  right){
            return;
        }
        int low = left;
        int high = right;
       int pivot = arr[low];
       while(low < high){
           //先让右边的动
           while(low < high && arr[high] >= pivot){
               high--;
           }
           arr[low] = arr[high];
           while(low < high && arr[low] <= pivot){
               low++;
           }
           arr[high] = arr[low];

       }
       arr[low] = pivot;
       //左右快速排序
        quickSort(arr,left,low-1);
        quickSort(arr,low+1,right);
    }
}
